package stalker.tester;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import message.Message;
import message.types.data.GetAllDatasAnswer;
import stalker.Stalker;

/**
 * Immutable class that identifies a user
 * <p>
 * Factory method
 */

public class TesterStalker extends Stalker {
  
  private final BlockingQueue<Message> queue_ = new ArrayBlockingQueue<Message>(50);
  
  public TesterStalker() {
    super("Tester Stalker");
  }
  
  /**
   * Accepts a request
   * @requires request not null
   * @param request A request made to the room
   */

  @Override
  public void acceptMessage(Message message) {
    queue_.add(message);
    assert message != null;
  }
  
  /**
   * Ensures that the next message is this
   */
  public boolean isMessage(Message message) {
    assert message != null;
    try {
      Message newm = queue_.take();
      System.out.println("received: "+ newm);
      System.out.println("compared with: "+ message);
      return newm.equals(message);
    } catch (InterruptedException e) {
      return false;
    }
  }
  
  /**
   * Ensures that the next message is of the type GetAllDatasAnswer
   */
  public boolean isMessageAllDatasAnswer() {
    try {
      Message newm = queue_.take();
      return newm instanceof GetAllDatasAnswer;
    } catch (InterruptedException e) {
      return false;
    }
  }
  
}
